package org.jsets.fastboot.quartz.config;

import org.jsets.fastboot.quartz.component.QuartzService;
//import org.jsets.fastboot.util.LogoUtils;
import org.jsets.fastboot.util.SpringContextHolder;
import org.quartz.JobListener;
import org.quartz.SchedulerListener;
import org.quartz.TriggerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import lombok.extern.slf4j.Slf4j;

/**
 * RUNNING
 *
 * @author wangjie (https://github.com/wj596)
 * @date 2021.07.12 21:49
 */
@Slf4j
public class QuartzRunner implements CommandLineRunner {

	@Autowired
	private ApplicationContext applicationContext;
	@Autowired
	private QuartzService quartzService;

	public void run(String... args) throws Exception {
		SpringContextHolder.setApplicationContextIfNecessary(applicationContext);
		SpringContextHolder.tryBeansThen(JobListener.class, bm -> {
			bm.keySet().forEach(b -> {
				log.info("Quartz注册JobListener[{}]", b);
				this.quartzService.addJobListener(bm.get(b));
			});
		});
		SpringContextHolder.tryBeansThen(SchedulerListener.class, bm -> {
			bm.keySet().forEach(b -> {
				log.info("Quartz注册JobListener[{}]", b);
				this.quartzService.addSchedulerListener(bm.get(b));
			});
		});
		SpringContextHolder.tryBeansThen(TriggerListener.class, bm -> {
			bm.keySet().forEach(b -> {
				log.info("Quartz注册JobListener[{}]", b);
				this.quartzService.addTriggerListener(bm.get(b));
			});
		});
		
		//LogoUtils.afterModuleLoad("Quartz");
	}

}